package com.glorysoft.lipuxinpda;


import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.glorysoft.lipuxinpda.activity.SettingActivity;
import com.glorysoft.lipuxinpda.base.App;
import com.glorysoft.lipuxinpda.base.BaseActivity;
import com.glorysoft.lipuxinpda.entity.BaseEntity;
import com.glorysoft.lipuxinpda.entity.LoginEntity;
import com.glorysoft.lipuxinpda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;
import com.glorysoft.lipuxinpda.base.Constant;

import java.util.HashMap;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private Context mContext;
    public Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
        mContext = this;
    }

    @Override
    protected int getLayoutResId() {
        return 0;
    }

    @Override
    protected void initView() {
        button = findViewById(R.id.btnLogin);
        button.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        Constant.INSTANCE.setNET_URL(FreeApi_SharePreferencesUtils.getSharePre("URL_FILE", "URL_IP", Constant.INSTANCE.getNET_URL(), App.Companion.getApp()));
        Constant.INSTANCE.setFILE_URL(FreeApi_SharePreferencesUtils.getSharePre("URL_FILE", "FILE_IP", Constant.INSTANCE.getFILE_URL(), App.Companion.getApp()));
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return null;
    }

    private void requestLogin() {
        FreeUI_EntityCallBack<BaseEntity<LoginEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<LoginEntity>>(new TypeToken<BaseEntity<LoginEntity>>(){}.getType(), getSupportFragmentManager(), this){
            @Override
            public void onSuccess(Response<BaseEntity<LoginEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getSupportFragmentManager())) {
                    showToast("登录成功",true);
                    Constant.INSTANCE.setLoginEntity(response.body().getData().getBody());
                    FreeApi_Utils.skipWithExtraIntent("", mContext, SettingActivity.class);
                    finish();
                }
            }

            @Override
            public void onError(Response<BaseEntity<LoginEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "PDALogin");
            }
        };

        HashMap<String, String> map = new HashMap<String ,String>();

        map.put("USERNAME", "ZZ");
        map.put("PASSWORD", "ZZ");
//        map["PASSWORD"] = etPassword!!.text.toString().trim { it <= ' ' }
        String method = Constant.INSTANCE.getNET_URL() + "/PDALogin";
        new NetUtils().requestPostNetNews(method, this, map, callBack);
    }

    @Override
    public void onClick(View v) {
        Log.i("TAG", "onClick: btn_login");
        if (v.getId() == R.id.btnLogin){
            requestLogin();
        }
    }
}